package cn.edu.cjw.APMFP.ui.dialog;

import cn.edu.cjw.APMFP.RoleRight.Service.RoleRightService;
import cn.edu.cjw.APMFP.RoleRight.Service.RrServiceImpl;
import cn.edu.cjw.APMFP.RoleRight.pojo.RoleRight;
import cn.edu.cjw.APMFP.Util.StringUtil;
import cn.edu.cjw.APMFP.ui.table.TableCallBack;
import cn.edu.cjw.APMFP.user.Service.UserService;
import cn.edu.cjw.APMFP.user.Service.UserServiceImpl;
import cn.edu.cjw.APMFP.user.pojo.User;
import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;


public class UserDialog extends JDialog {

	private JButton confirmBtn = new JButton("确认");
	private JButton cancelBtn = new JButton("取消");

	private UserService service = new UserServiceImpl();

	private RoleRightService roleService = new RrServiceImpl();

	private User entity;

	private TableCallBack callBack;

	private List<RoleRight> roleList = new ArrayList<>();

	public UserDialog(User entity, TableCallBack callBack){
		this.entity = entity;
		this.callBack = callBack;
		this.setTitle(entity != null ? "编辑" : "新增");
		this.setSize(500, 700);
		this.init();
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setLocationRelativeTo(null);
//		this.pack();
		this.setVisible(true);
	}

	private void init(){
		this.setLayout(new BorderLayout());

		try {
			roleList.addAll(roleService.showAll());
		} catch (Exception e) {
			e.printStackTrace();
		}

		GridBagLayout gridBagLayout = new GridBagLayout();
		JPanel centerPanel = new JPanel(gridBagLayout);
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.insets = new Insets(10, 10, 10, 10);
		constraints.ipady = 10;

		JLabel idLabel = new JLabel("账号:");
		JTextField idField = new JTextField(27);
		constraints.gridx = 0;
		constraints.gridy = 0;
		centerPanel.add(idLabel, constraints);
		constraints.gridx = 1;
		centerPanel.add(idField, constraints);

		JLabel pwdLabel = new JLabel("密码:");
		JPasswordField pwdField = new JPasswordField(27);
		constraints.gridx = 0;
		constraints.gridy = 1;
		centerPanel.add(pwdLabel, constraints);
		constraints.gridx = 1;
		centerPanel.add(pwdField, constraints);

		JLabel nameLabel = new JLabel("名称:");
		JTextField nameField = new JTextField(27);
		constraints.gridx = 0;
		constraints.gridy = 2;
		centerPanel.add(nameLabel, constraints);
		constraints.gridx = 1;
		centerPanel.add(nameField, constraints);

		JLabel rightLabel = new JLabel("角色:");
		JComboBox<String> rightField = new JComboBox<>();
		roleList.forEach(role -> {
			rightField.addItem(role.getRName());
		});
		rightField.setPreferredSize(nameField.getPreferredSize());
		constraints.gridx = 0;
		constraints.gridy = 3;
		centerPanel.add(rightLabel, constraints);
		constraints.gridx = 1;
		centerPanel.add(rightField, constraints);

		JLabel ageLabel = new JLabel("年龄:");
		JTextField ageField = new JTextField(27);
		constraints.gridx = 0;
		constraints.gridy = 4;
		centerPanel.add(ageLabel, constraints);
		constraints.gridx = 1;
		centerPanel.add(ageField, constraints);

		JLabel sexLabel = new JLabel("性别:");
		JComboBox<String> sexField = new JComboBox<>();
		sexField.addItem("男");
		sexField.addItem("女");
		sexField.setPreferredSize(nameField.getPreferredSize());
		constraints.gridx = 0;
		constraints.gridy = 5;
		centerPanel.add(sexLabel, constraints);
		constraints.gridx = 1;
		centerPanel.add(sexField, constraints);

		JLabel statusLabel = new JLabel("状态:");
		statusLabel.setVisible(false);
		JComboBox<String> statusField = new JComboBox<>();
		statusField.setVisible(false);
		statusField.addItem("正常");
		statusField.addItem("停用");
		statusField.setSelectedIndex(0);
		statusField.setPreferredSize(nameField.getPreferredSize());
		constraints.gridx = 0;
		constraints.gridy = 6;
		centerPanel.add(statusLabel, constraints);
		constraints.gridx = 1;
		centerPanel.add(statusField, constraints);

		if(entity != null){
			statusLabel.setVisible(true);
			statusField.setVisible(true);
			idField.setEditable(false);
			idField.setText(entity.getAccount());
			nameField.setText(entity.getName());
			pwdField.setText(entity.getPassWord());
			rightField.setSelectedItem(entity.getuRole().getRName());
			ageField.setText(entity.getAge() + "");
			statusField.setSelectedItem(entity.isDeactivate() ? "停用" : "正常");
		}

		this.add(centerPanel, BorderLayout.CENTER);

		JPanel footerPanel = new JPanel();
		footerPanel.setPreferredSize(new Dimension(this.getWidth(), 100));
		footerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 10));
		footerPanel.add(confirmBtn);
		footerPanel.add(cancelBtn);
		confirmBtn.addActionListener(e -> {
			String id = idField.getText().trim();
			if(!StringUtil.isNotBlank(id)){
				JOptionPane.showMessageDialog(null, "账号不能为空");
				return;
			}
			String name = nameField.getText().trim();
			if(!StringUtil.isNotBlank(name)){
				JOptionPane.showMessageDialog(null, "名称不能为空");
				return;
			}
			String pwd = new String(pwdField.getPassword()).trim();
			if(!StringUtil.isNotBlank(pwd) && entity == null){
				JOptionPane.showMessageDialog(null, "请输入密码");
				return;
			}
			if(!StringUtil.isNotBlank(pwd) && entity != null){
				pwd = entity.getPassWord();
			}
			int index = rightField.getSelectedIndex();
			if(index == -1){
				JOptionPane.showMessageDialog(null, "请选择角色");
				return;
			}
			Object sex = sexField.getSelectedItem();
			if(sex == null){
				JOptionPane.showMessageDialog(null, "请选择性别");
				return;
			}
			String age = ageField.getText().trim();
			if(!StringUtil.isInt(age)){
				JOptionPane.showMessageDialog(null, "年龄不合法");
				return;
			}
			try {
				if(entity == null){
					User r = new User();
					r.setAccount(id);
					r.setName(name);
					r.setuRole(roleList.get(index));
					r.setGender(sex.toString());
					r.setAge(Integer.parseInt(age));
					service.addUser(r);
				}else{
					int i = statusField.getSelectedIndex();
					service.updateUser(entity, pwd, name, roleList.get(index), sex.toString(), Integer.parseInt(age), i == 1);
				}
			} catch (Exception ex) {
				JOptionPane.showMessageDialog(null, "操作失败, 原因:" + ex.getMessage());
				return;
			}
			if(callBack != null){
				callBack.refreshTable();
			}
			JOptionPane.showMessageDialog(null, "操作成功");
			close();
		});
		cancelBtn.addActionListener(e -> {
			close();
		});
		this.add(footerPanel, BorderLayout.SOUTH);
	}
	
	private void close(){
		this.dispose();
	}
	
	public static void main(String[] args) {
		new UserDialog(null, null);
	}

}
